package com.youdianstar.app.ui.reward

import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.youdianstar.app.entity.*
import com.youdianstar.app.support.ApiRepository
import com.youdianstar.app.common.base.BaseViewModel
import com.youdianstar.app.entity.CouponInfo
import com.youdianstar.app.entity.CouponRes
import com.youdianstar.app.common.event.NetEventLiveData
import com.youdianstar.app.common.event.NetStateLiveData

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch


/**
 * File: RewardViewModel
 * Author: 82149 Create: 2022/2/16 10:00
 * Changes (from 2022/2/16)
 * --------------------------------------------------
 * description: 积分,优惠券,相关记录
 * ---------------------------------------------------
 *
 */
class RewardViewModel(private val repo: ApiRepository) : BaseViewModel() {

    //优惠券列表 - 账户记录
    val couponResLiveData = NetStateLiveData<List<CouponRes>>()

    /**
     * 获得优惠券列表-我的优惠券-优惠券主页
     *
     * @param type 0-通用优惠券 1-商家优惠券
     * @param state  1-未使用 2-已使用 3-已过期
     * @param offset 偏移量 0开始
     */
    fun getCouponsRecord(type: Int?=null,state: Int?=null,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getCouponsRecord(type = type, state = state, offset = offset, couponResLiveData)
        }
    }

    /**
     * 获得优惠码列表-我的优惠码(优惠券主页)
     *
     * @param offset 偏移量 0开始
     */
    fun getPromoCodeRecord(offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getPromoCodeRecord(offset = offset, couponResLiveData)
        }
    }



    val redeemPromoResultData = NetStateLiveData<RedeemResult>()
    /**
     * 优惠码兑换优惠券
     *
     * @param code 优惠码
     */
    fun redeemCouponWithPromo(code:String){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.redeemCouponWithPromo(code,redeemPromoResultData)
        }
    }


    val redeemPagingLiveData = NetEventLiveData()
    /**
     * 优惠券中心-优惠券列表:
     * 分页记录
     * @param type 0-通用优惠券 1-商家优惠券
     */
    fun getRedeemCouponsPaging(type: Int): Flow<PagingData<CouponInfo>> =
        repo.getRedeemCouponsPaging(type,redeemPagingLiveData).cachedIn(viewModelScope)


    val redeemPointResultData = NetStateLiveData<Any>()
    /**
     * 积分兑换优惠券
     *
     * @param id
     */
    fun redeemCouponWithPoints(id:Int){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.redeemCouponInPoints(id,redeemPointResultData)
        }
    }

    val pointAccountLiveData = NetStateLiveData<PointAccount>()
    /**
     * 获取积分账户
     */
    fun getPointAccount(isShow: Boolean = false){
        if (isShow)
            showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getMyPoints(pointAccountLiveData)
        }
    }


    val pointPlansLiveData = NetStateLiveData<List<PointPlan>>()
    /**
     * 获取积分规则
     * -列表数据
     */
    fun getPointPlansInfo(){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getPointPlans(pointPlansLiveData)
        }
    }

    val pointBindCardActionData = NetStateLiveData<List<PointDbResult>>()
    fun checkPointsBindCard(isShow: Boolean = false){
        if (isShow)
            showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.checkPointsBindAction(pointBindCardActionData)
        }
    }

    val pointsPagingLiveData = NetEventLiveData()
    /**
     * 获取积分记录
     * 分页记录
     * @param type  1-增加 2-使用
     */
    fun getPointRecordPaging(type: Int): Flow<PagingData<PointRecord>> =
        repo.getPointRecordPaging(type,pointsPagingLiveData).cachedIn(viewModelScope)

    val invitePlansLiveData = NetStateLiveData<List<InviteInfo>>()
    /**
     * 获取邀请规则
     * -列表数据,取单个
     */
    fun getInvitePlans(){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getInvitePlans(invitePlansLiveData)
        }
    }

    val redeemInviteResultData = NetStateLiveData<Any>()
    /**
     * 邀请-兑换邀请码
     *
     * @param code 邀请码
     */
    fun redeemInvitationCode(code:String){
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.redeemInvitationCode(code,redeemInviteResultData)
        }
    }


    val inviteRecordLiveData = NetStateLiveData<List<InviteRc>>()

    /**
     * 邀请--获取邀请记录
     *
     * @param offset 偏移量 0开始
     */
    fun getInvitationRecord(offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getInvitationRecord( offset = offset, inviteRecordLiveData)
        }
    }

    /*****************************充电卡**********************************/
    //优惠券列表 - 账户记录
    val chargePlansLiveData = NetStateLiveData<List<ChargePlan>>()
    /**
     * 充点卡列表-列表主页
     *
     * @param type 1-年卡 2-季卡 3-月卡 4-周卡
     * @param offset 偏移量 0开始
     */
    fun getChargePlanList(type: Int?=null,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getChargePlanList(offset = offset, chargePlansLiveData)
        }
    }

    /**
     * 充点卡列表-我的充电卡
     *
     * @param type 1-年卡 2-季卡 3-月卡 4-周卡
     * @param offset 偏移量 0开始
     */
    fun getChargePlanRecord(type: Int?=null,offset: Int){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getChargePlanRecord(offset = offset, chargePlansLiveData)
        }
    }

    /***************************************************************/



    val recordPagingLiveData = NetEventLiveData()
    /**
     * 获得优惠码兑换记录列表
     * 分页记录
     */
    fun getPromoRecordPaging(): Flow<PagingData<PromoRecord>> =
        repo.getPromoRecordPaging(recordPagingLiveData).cachedIn(viewModelScope)

}